package cem.agony.ui.controller;

import java.util.Calendar;

import cem.agony.AgonyTracker;
import cem.agony.model.Rider;
import cem.agony.util.AgonyUtil;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Duration;

public class RiderOverviewController
{
    @FXML
    private TableView<Rider> riderTable;
    @FXML
    private TableColumn<Rider, Integer> numberColumn;
    @FXML
    private TableColumn<Rider, String> firstNameColumn;
    @FXML
    private TableColumn<Rider, String> lastNameColumn;

    @FXML
    private Label statusLabel;
    @FXML
    private Label timeLabel;

    @FXML
    private Label totalMilesLabel;
    @FXML
    private Label totalRaisedLabel;
    @FXML
    private Label flatDonationsLabel;
    @FXML
    private Label perMileLabel;

    private AgonyTracker mainApp;

    public RiderOverviewController()
    {
    }

    @FXML
    private void initialize()
    {
        numberColumn.setCellValueFactory( new PropertyValueFactory<Rider, Integer>( "number" ) );
        firstNameColumn.setCellValueFactory( new PropertyValueFactory<Rider, String>( "firstName" ) );
        lastNameColumn.setCellValueFactory( new PropertyValueFactory<Rider, String>( "lastName" ) );
        riderTable.setColumnResizePolicy( TableView.CONSTRAINED_RESIZE_POLICY );

        showRiderDetails( null );

        riderTable.getSelectionModel()
            .selectedItemProperty()
            .addListener( new ChangeListener<Rider>()
            {
                @Override
                public void changed( ObservableValue<? extends Rider> observable, Rider oldValue,
                                     Rider newValue )
                {
                    showRiderDetails( newValue );
                }
            } );

        Timeline timeline = new Timeline( new KeyFrame( Duration.seconds( 0 ),
                                                        new EventHandler<ActionEvent>()
                                                        {
                                                            @Override
                                                            public void handle( ActionEvent event )
                                                            {
                                                                Calendar time = Calendar.getInstance();
                                                                String hourString = AgonyUtil.pad( 2,
                                                                                                   ' ',
                                                                                                   time.get( Calendar.HOUR ) == 0 ? "12" : Integer.toString( time.get( Calendar.HOUR ) ) );
                                                                String minuteString = AgonyUtil.pad( 2,
                                                                                                     '0',
                                                                                                     Integer.toString( time.get( Calendar.MINUTE ) ) );
                                                                String secondString = AgonyUtil.pad( 2,
                                                                                                     '0',
                                                                                                     Integer.toString( time.get( Calendar.SECOND ) ) );
                                                                String ampmString = time.get( Calendar.AM_PM ) == Calendar.AM ? "AM" : "PM";
                                                                timeLabel.setText( hourString +
                                                                                   ":" +
                                                                                   minuteString +
                                                                                   ":" +
                                                                                   secondString +
                                                                                   " " +
                                                                                   ampmString );
                                                            }
                                                        } ),
                                          new KeyFrame( Duration.seconds( 1 ) ) );
        timeline.setCycleCount( Animation.INDEFINITE );
        timeline.play();
    }

    public void setMainApp( AgonyTracker mainApp )
    {
        this.mainApp = mainApp;
        riderTable.setItems( this.mainApp.getRiderData() );
    }

    private void showRiderDetails( Rider rider )
    {
        if ( rider == null )
        {
            statusLabel.setText( "" );
            totalMilesLabel.setText( "" );
            totalRaisedLabel.setText( "" );
            flatDonationsLabel.setText( "" );
            perMileLabel.setText( "" );
        }
        else
        {
//            statusLabel.setText( rider.getFullName() +
//                                 " is " +
//                                 rider.getLatestStatus().toStringWithTime() );
            totalMilesLabel.setText( Integer.toString( rider.getMiles() ) );
            totalRaisedLabel.setText( Double.toString( rider.getTotalRaised() ) );
            flatDonationsLabel.setText( Double.toString( rider.getTotalFlatRateAmount() ) );
            perMileLabel.setText( Double.toString( rider.getTotalPerMileAmount() ) );
        }
    }
}